package com.edu.service;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

import java.util.concurrent.TimeUnit;

public class CaffeineCacheExample {  
    public static void main(String[] args) {  
        // 创建一个Caffeine缓存实例  
        Cache<String, String> cache = Caffeine.newBuilder()  
                .maximumSize(100) // 设置缓存的最大容量  
                .expireAfterWrite(10, TimeUnit.MINUTES) // 设置缓存条目的过期时间为10分钟
                .build();  
  
        // 将数据存入缓存  
        String key = "exampleKey";  
        String value = "exampleValue";  
        cache.put(key, value);  
  
        // 从缓存中获取数据并打印  
        String cachedValue = cache.getIfPresent(key);  
        if (cachedValue != null) {  
            System.out.println("Retrieved value from cache: " + cachedValue);  
        } else {  
            System.out.println("Value not found in cache.");  
        }  
    }  
}